Imports LookUp
Imports System.Data

Partial Class WebForms_Add_CInfo
    Inherits WebFormCommon

    Private Sub SetPageSecurity()
        Dim sec As New Secure
        Dim cfunc As New CIMSfunc

        sec.ParentPage = ParentPage

        If IsNothing(ViewState("CID")) Then
            sec.AddControl(Me.Save_btn, Secure.ControlTypes.AddType)
        Else
            sec.AddControl(Me.Save_btn, Secure.ControlTypes.EditType)
        End If

        sec.SetPageAccess(Session("UserName"))

        If ViewState("mode") = "view" Then
            cfunc.FormDisable(divMain)
        End If

        cfunc = Nothing
        sec = Nothing

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim lookup As New FillLookUp

            Dim cf As New CIMSfunc

            cf.setNumericFormat(Income_txt)
            cf.setNumericFormat(NetWorth_txt)
            cf = Nothing

            With lookup
                .FillDropDown(Salutation, FillLookUp.eLookUpType.other, "SALUTATION")
                .FillDropDown(MaritalStatus_cmb, FillLookUp.eLookUpType.other, "MARITAL_STATUS")
                .FillDropDown(Gender_cmb, FillLookUp.eLookUpType.other, "GENDER")
                .FillDropDown(Me.Country_cmb, FillLookUp.eLookUpType.country)
                .FillDropDown(Me.City_cmb, FillLookUp.eLookUpType.country_city, , Country_cmb.SelectedValue.ToString)
            End With

            lookup = Nothing

            Call InitVars()

            Policy_td.InnerText = ViewState("PolicyNo")
            Provider_td.InnerText = ViewState("Provider")

            If IsNothing(ViewState("CID")) Then
                Other_Info_btn.Visible = False
                Correspondence_btn.Visible = False
                Attachments_btn.Visible = False
                Risk_Profile_btn.Visible = False
            Else
                Call LoadClientInfo()
            End If

            Call SetPageSecurity()
        End If


    End Sub

    Private Sub LoadClientInfo()
        Dim dao As New DAOMain.POLICY_CLIENT_DTL_DAO_MAIN
        Dim vo As New DAOMain.POLICY_CLIENT_DTL_VO_MAIN
        Dim cf As New CIMSfunc
        Dim lookup As New LookUp.FillLookUp

        vo.POLICY_CLIENT_CD = ViewState("CID")
        vo.POLICY_SEQ_CD = ViewState("Seq_CD")


        Dim dt As DataTable = dao.Search(vo)

        On Error Resume Next

        If dt.Rows.Count > 0 Then
            With dt.Rows(0)
                Dim iInsured As Integer = 0
                Dim iOwner As Integer = 0

                Salutation.SelectedValue = .Item("POLICY_CLIENT_SALUTATION")
                FirstName_txt.Text = .Item("POLICY_CLIENT_FIRST_NAME")
                MiddleName_txt.Text = .Item("POLICY_CLIENT_MIDDLE_NAME")
                LastName_txt.Text = .Item("POLICY_CLIENT_LAST_NAME")
                Gender_cmb.SelectedValue = .Item("GENDER")
                ViewState.Add("CName", LastName_txt.Text & ", " & FirstName_txt.Text & " " & MiddleName_txt.Text)

                Alias_txt.Text = .Item("POLICY_CLIENT_ALIAS")

                Birthdate_txt.Text = cf.ParseGetDate(.Item("BIRTHDAY"))
                Birthplace_txt.Text = .Item("BIRTHPLACE")
                Add1_txt.Text = .Item("POLICY_CLIENT_ADDRESS")
                Add2_txt.Text = .Item("POLICY_CLIENT_ADDRESS2")

                Country_cmb.SelectedValue = .Item("COUNTRY_CD")
                lookup.FillDropDown(City_cmb, FillLookUp.eLookUpType.country_city, , Country_cmb.SelectedValue.ToString)

                City_cmb.SelectedValue = .Item("CITY_CD")
                Nationality_txt.Text = .Item("NATIONALITY_CD")
                Residence_txt.Text = .Item("RESIDENCY")
                Email_txt.Text = .Item("EMAIL_ADDRESS")
                Zip_txt.Text = .Item("ZIP_CD")
                MaritalStatus_cmb.SelectedValue = .Item("MARITAL_ST")

                iInsured = .Item("insured_tag")
                iOwner = .Item("owner_tag")

                If iInsured <> 0 Then Me.chInsured.Checked = True
                If iOwner <> 0 Then Me.chOwner.Checked = True

                NetWorth_txt.Text = cf.loadFormat(.Item("NET_WORTH"))
                Income_txt.Text = cf.loadFormat(.Item("EMP_INCOME"))

                T1_txt.Text = cf.ParsePhoneNo(.Item("TEL_NO"), 1)
                T2_txt.Text = cf.ParsePhoneNo(.Item("TEL_NO"), 2)
                T3_txt.Text = cf.ParsePhoneNo(.Item("TEL_NO"), 3)
                T4_txt.Text = cf.ParsePhoneNo(.Item("TEL_NO"), 4)

                M1_txt.Text = cf.ParsePhoneNo(.Item("MOBILE_NO"), 1)
                M2_txt.Text = cf.ParsePhoneNo(.Item("MOBILE_NO"), 2)
                M3_txt.Text = cf.ParsePhoneNo(.Item("MOBILE_NO"), 3)

                F1_txt.Text = cf.ParsePhoneNo(.Item("FAX_NO"), 1)
                F2_txt.Text = cf.ParsePhoneNo(.Item("FAX_NO"), 2)
                F3_txt.Text = cf.ParsePhoneNo(.Item("FAX_NO"), 3)
                F4_txt.Text = cf.ParsePhoneNo(.Item("FAX_NO"), 4)

            End With
        End If

        lookup = Nothing
        dao = Nothing
        vo = Nothing
        cf = Nothing
    End Sub

    Protected Sub Country_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Country_cmb.SelectedIndexChanged
        Dim lookup As New LookUp.FillLookUp

        lookup.FillDropDown(City_cmb, FillLookUp.eLookUpType.country_city, , Country_cmb.SelectedValue.ToString)
        lookup = Nothing
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim dao As New DAOMain.POLICY_CLIENT_DTL_DAO_MAIN
        Dim vo As New DAOMain.POLICY_CLIENT_DTL_VO_MAIN
        Dim polyFunc As New PolyFunc.ASPFunc
        Dim cf As New CIMSfunc
        Dim iInsured As Integer = 0
        Dim iOwner As Integer = 0

        With vo
            .POLICY_SEQ_CD = ViewState("Seq_CD")

            .POLICY_CLIENT_SALUTATION = Salutation.SelectedValue.ToString
            .POLICY_CLIENT_FIRST_NAME = FirstName_txt.Text
            .POLICY_CLIENT_MIDDLE_NAME = MiddleName_txt.Text
            .POLICY_CLIENT_LAST_NAME = LastName_txt.Text
            .POLICY_CLIENT_ALIAS = Alias_txt.Text

            .MARITAL_ST = MaritalStatus_cmb.SelectedValue.ToString
            .BIRTHDAY = cf.ParseSetDate(Birthdate_txt.Text)
            .BIRTHPLACE = Birthplace_txt.Text
            .POLICY_CLIENT_ADDRESS = Add1_txt.Text
            .POLICY_CLIENT_ADDRESS2 = Add2_txt.Text
            .CITY_CD = City_cmb.SelectedValue.ToString
            .COUNTRY_CD = Country_cmb.SelectedValue.ToString
            .NATIONALITY_CD = Nationality_txt.Text 'TODO: make dropdown
            .RESIDENCY = Val(Residence_txt.Text)
            .GENDER = Gender_cmb.SelectedValue.ToString
            .TEL_NO = T1_txt.Text & "-" & T2_txt.Text & "-" & T3_txt.Text & "-" & T4_txt.Text
            .MOBILE_NO = M1_txt.Text & "-" & M2_txt.Text & "-" & M3_txt.Text
            .FAX_NO = F1_txt.Text & "-" & F2_txt.Text & "-" & F3_txt.Text & "-" & F4_txt.Text
            .EMAIL_ADDRESS = Email_txt.Text
            .ZIP_CD = Zip_txt.Text
            If Trim(NetWorth_txt.Text) = "" Then NetWorth_txt.Text = "0"
            If Trim(Income_txt.Text) = "" Then Income_txt.Text = "0"

            .NET_WORTH = cf.saveFormat(NetWorth_txt.Text)
            .EMP_INCOME = cf.saveFormat(Income_txt.Text)

            If chInsured.Checked Then iInsured = 1
            If chOwner.Checked Then iOwner = 1

            .INSURED_TAG = iInsured
            .OWNER_TAG = iOwner

            Select Case IsNothing(ViewState("CID"))
                Case True
                    Dim dt As DataTable = dao.ExecQuery("Select isnull(max(convert(numeric, policy_client_cd)),0) +1 as ID From policy_client_dtl")

                    .POLICY_CLIENT_CREA_BY = Session("UserName")
                    .POLICY_CLIENT_CREA_DT = Now
                    .POLICY_CLIENT_CD = dt.Rows(0)("ID")

                    If dao.Create_Record(vo) Then
                        lblID.Text = .POLICY_CLIENT_CD
                        polyFunc.MessageAlert(ParentPage, "Record Saved!")

                        Other_Info_btn.Visible = True
                        Correspondence_btn.Visible = True
                        Attachments_btn.Visible = True
                        Risk_Profile_btn.Visible = True

                        ViewState.Add("CID", dt.Rows(0)("ID"))
                        ViewState.Add("CName", LastName_txt.Text & ", " & FirstName_txt.Text & " " & MiddleName_txt.Text)

                    Else
                        polyFunc.MessageAlert(ParentPage, "Error Saving Record!")
                    End If

                Case Else
                    .POLICY_CLIENT_LAST_UPD_BY = Session("UserName")
                    .POLICY_CLIENT_LAST_UPD_DT = Now
                    .POLICY_CLIENT_CD = ViewState("CID")

                    If dao.Update(vo) Then
                        polyFunc.MessageAlert(ParentPage, "Record Saved!")
                        If Not IsNothing(ViewState("CName")) Then ViewState.Remove("CName")
                        ViewState.Add("CName", LastName_txt.Text & ", " & FirstName_txt.Text & " " & MiddleName_txt.Text)
                    Else
                        polyFunc.MessageAlert(ParentPage, "Error Saving Record!")
                    End If
            End Select
        End With

        polyFunc = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        InitSessions()
        Session.Remove("CID")
        Session.Remove("CName")

        Select Case Me.PageControlMode
            Case ControlMode.NewBusiness
                Response.Redirect("~/New_Business/ClientInfo.aspx")
            Case ControlMode.ClientServicing
                Response.Redirect("~/Client_Servicing/ClientInfo.aspx")
        End Select

    End Sub

    Protected Sub Correspondence_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Correspondence_btn.Click
        InitSessions()
        Select Case Me.PageControlMode
            Case ControlMode.NewBusiness
                Response.Redirect("~/New_Business/Add_CInfo_Cor.aspx")
            Case ControlMode.ClientServicing
                Response.Redirect("~/Client_Servicing/Add_CInfo_Cor.aspx")
        End Select

    End Sub

    Protected Sub Other_Info_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Other_Info_btn.Click
        InitSessions()
        Select Case Me.PageControlMode
            Case ControlMode.NewBusiness
                Response.Redirect("~/New_Business/Add_CInfo_Other.aspx")
            Case ControlMode.ClientServicing
                Response.Redirect("~/Client_Servicing/Add_CInfo_Other.aspx")
        End Select

    End Sub

    Protected Sub Risk_Profile_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Risk_Profile_btn.Click
        InitSessions()
        Select Case Me.PageControlMode
            Case ControlMode.NewBusiness
                Response.Redirect("~/New_Business/Add_CInfo_Risk.aspx")
            Case ControlMode.ClientServicing
                Response.Redirect("~/Client_Servicing/Add_CInfo_Risk.aspx")
        End Select

    End Sub

    Protected Sub Attachments_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Attachments_btn.Click
        InitSessions()
        Select Case Me.PageControlMode
            Case ControlMode.NewBusiness
                Response.Redirect("~/New_Business/Add_CInfo_Att.aspx")
            Case ControlMode.ClientServicing
                Response.Redirect("~/Client_Servicing/Add_CInfo_Att.aspx")
        End Select

    End Sub

    Protected Sub lbtnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnSearch.Click
        ClientSearch1.Visible = True
    End Sub

    Protected Sub ClientSearch_Policy_Selected() Handles ClientSearch1.Policy_Selected
        Dim cFunc As New CIMSfunc
        Dim polyFunc As New PolyFunc.ASPFunc
        Dim LookUp As New LookUp.FillLookUp

        With ClientSearch1
            Birthdate_txt.Text = cFunc.ParseGetDate(.dtBday)
            Country_cmb.SelectedValue = .strCountry
            LookUp.FillDropDown(City_cmb, LookUp.eLookUpType.country_city, , Country_cmb.SelectedValue.ToString)

            City_cmb.SelectedValue = .strCity
            Add1_txt.Text = .strAdd1
            Add2_txt.Text = .strAdd2
            FirstName_txt.Text = .strFirstName
            LastName_txt.Text = .strLastName
            MiddleName_txt.Text = .strMiddleName
            Gender_cmb.SelectedValue = .strGender

            .Visible = False
        End With

        LookUp = Nothing
        cFunc = Nothing
        polyFunc = Nothing
    End Sub
End Class
